/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ing.sisval.gui.reportes;

import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import org.ing.sisval.be.BEPropiedad;
import org.ing.sisval.bl.BLPropiedad;
import org.ing.sisval.bl.BLTerreno;
import org.ing.sisval.ds.DSUtil;
import org.ing.sisval.gui.JFPrincipal;
import org.ing.sisval.util.UtilPropiedad;
import org.rx.cr.report.ReportGeneric;
import org.rx.cr.util.Utilitarios;

/**
 *
 * @author Mysham
 */
public class JIFReportePropiedadComercial extends javax.swing.JInternalFrame {

    /**
     * Creates new form JIFReportePropiedadComercial
     */
    JFPrincipal parent;
    UtilPropiedad utilPropiedad;
    BLTerreno blTerreno;
    BLPropiedad blPropiedad;

    public JIFReportePropiedadComercial(JFPrincipal parent) {
        initComponents();
        this.parent = parent;
        utilPropiedad = new UtilPropiedad(parent.getConf());
        blTerreno = new BLTerreno(parent.getConf());
        blPropiedad = new BLPropiedad(parent.getConf());
        cargarCombo();
        txfDireccion.setEditable(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jcbPropietario = new javax.swing.JComboBox();
        jbGenerar = new javax.swing.JButton();
        txfDireccion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jbActualizarComboProveedor = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("INFORME PERICIAL");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INFORME PERICIAL", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jcbPropietario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbPropietario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPropietarioActionPerformed(evt);
            }
        });

        jbGenerar.setText("Generar");
        jbGenerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGenerarActionPerformed(evt);
            }
        });

        txfDireccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfDireccionActionPerformed(evt);
            }
        });

        jLabel1.setText("Propietario :");

        jLabel2.setText("Dirección :");

        jbActualizarComboProveedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/small_refresh.png"))); // NOI18N
        jbActualizarComboProveedor.setBorder(null);
        jbActualizarComboProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbActualizarComboProveedorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txfDireccion)
                    .addComponent(jcbPropietario, 0, 381, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbGenerar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbActualizarComboProveedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPropietario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbGenerar)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txfDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(jbActualizarComboProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbGenerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGenerarActionPerformed
        // TODO add your handling code here:
        generarReporte();
    }//GEN-LAST:event_jbGenerarActionPerformed

    private void txfDireccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfDireccionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfDireccionActionPerformed

    private void jbActualizarComboProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbActualizarComboProveedorActionPerformed
        // TODO add your handling code here:
        try {
            utilPropiedad.cargarPropiedad(jcbPropietario);
            //jbActualizarComboProveedor.setEnabled(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al cargar el proveedor", "Administrar pc/lap/ser", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jbActualizarComboProveedorActionPerformed

    private void jcbPropietarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbPropietarioActionPerformed
        // TODO add your handling code here:
        txfDireccion.setText("");
        if (jcbPropietario.getSelectedIndex() > 0) {
            txfDireccion.setText(((BEPropiedad) jcbPropietario.getSelectedItem()).getDireccion());
        }

    }//GEN-LAST:event_jcbPropietarioActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbActualizarComboProveedor;
    private javax.swing.JButton jbGenerar;
    private javax.swing.JComboBox jcbPropietario;
    private javax.swing.JTextField txfDireccion;
    // End of variables declaration//GEN-END:variables
private boolean isDatosLlenos() {
        return Utilitarios.isDatosLlenos(new Object[][]{
            {jcbPropietario, "Seleccione el nombre del propietario."}
        });
    }

    private void resetComponent() {
        Utilitarios.resetComponents(new Object[]{
            jcbPropietario, txfDireccion
        });
        txfDireccion.setEditable(false);
    }

    private void generarReporte() {
        if (isDatosLlenos()) {
            int id = ((BEPropiedad) jcbPropietario.getSelectedItem()).getPropiedadid();

            String url = "";

            try {
                switch (blTerreno.buscar(blPropiedad.buscar(id).getTerrenoid()).getTipovaluacion().toLowerCase()) {
                    case "comercial":
                        url = "/org/ing/sisval/report/TerrenoComercial.jasper";
                        break;
                    case "reglamentaria":
                        try {
                            switch (blTerreno.buscar(blPropiedad.buscar(id).getTerrenoid()).getTipolote().toLowerCase()) {

                                case "con un solo frente a la via publica":
                                    try {
                                        switch (blTerreno.buscar(blPropiedad.buscar(id).getTerrenoid()).getCaracteristicalote().toLowerCase()) {
                                            case "rectangular":
                                                url = "/org/ing/sisval/report/TerrenoSoloFrenteViaPublica.jasper";
                                                break;
                                            case "irregular":
                                                url = "/org/ing/sisval/report/TerrenoComercial.jasper";
                                                break;
                                        }
                                    } catch (Exception e) {
                                      
                                    }
                                    break;
                                case "con mas de un frente a la via publica":
                                    url = "/org/ing/sisval/report/TerrenoMasFrenteViaPublica.jasper";
                                    break;
                                case "con frente a un pasadizo comun o via de dominio privado en condominio":
                                    url = "/org/ing/sisval/report/TerrenoFrentePasadizoComun.jasper";
                                    break;
                                case "sin acceso por via publica ni privada sino por predio de propiedad de terceros":
                                    url = "/org/ing/sisval/report/TerrenoSinAccesoViaPublica.jasper";
                                    break;
                                case "restriccion de uso que soporte un predio, derivado de la servidumbre a favor de terceros.":
                                    url = "/org/ing/sisval/report/TerrenoRestriccionUso.jasper";
                                    break;
                                case "lote con frente inferior al normativo vigente":
                                    url = "/org/ing/sisval/report/TerrenoConFrenteInferiorNormativo.jasper";
                                    break;
                                case "lote con profundidad menor de quince metros":
                                    url = "/org/ing/sisval/report/TerrenoConProfundidad15.jasper";
                                    break;
                                case "situado parte en tierra firme y parte en mar":
                                    url = "/org/ing/sisval/report/TerrenoParteTierraMar.jasper";
                                    break;

                            }
                        } catch (Exception e) {
                            JOptionPane.showMessageDialog(this, "No se pudo identificar en calculo reglamentario de terreno.", "Atención", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "No se pudo identificar el tipo de valuación de terreno.", "Atención", JOptionPane.INFORMATION_MESSAGE);

            }

            try {
                ReportGeneric report = new ReportGeneric(DSUtil._DSConneccion(parent.getConf()).getConeccion());

                report.setReportParent("/org/ing/sisval/report/");
                JInternalFrame jif = report.mkReportToInternalFrame("ReportePericialComercial",
                        new String[]{"propiedadid", "terreno"},
                        new Object[]{id,
                            this.getClass().getResourceAsStream(url)},
                        "Informe pericial");
                parent.insertarInternalFrame(jif);
                jif.setMaximum(true);
                resetComponent();

            } catch (PropertyVetoException ex) {
                JOptionPane.showMessageDialog(this, "Error al generar el reporte. Inténtelo nuevamente.", "Atencion", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(JFPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Ingrese datos!", "Atencion", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void cargarCombo() {
        try {
            utilPropiedad.cargarPropiedad(jcbPropietario);

        } catch (Exception ex) {
            Logger.getLogger(JIFReportePropiedadComercial.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }
}
